<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>

<body>
    <!-- vue.js 是采用数据劫持结合发布者-订阅者模式的方式，通过Object.defineProperty()来劫持各个属性的setter，getter，在数据变动时发布消息给订阅者，触发相应的监听回调。 -->
    <input type="text" id="text">
    <span id="box"></span>
    <script>
        var obj = {
            val: "haogu"
        }

        // Object.defineProperty(对象,属性,配置信息)
        Object.defineProperty(obj, "val", {
            get: function () {
                console.log("get");

                return val
            },
            set: function (newVal) {
                val = newVal
                document.getElementById("text").value = val
                document.getElementById("box").innerHTML = val
                console.log("set");
            }
        })

        document.addEventListener("keyup", function (e) {
            console.log(e.target);
            obj.val = e.target.value


        }, false)

        // console.log(obj.val);
        // obj.val = "erge"





    </script>


</body>

</html>